const DB = require('../tools/mysql')
const path = require('path')
const fs = require('fs')

module.exports = async ctx => {
    ctx.request.body = ctx.req.body ? ctx.req.body : ctx.request.body
    if (ctx.request.body.code == undefined) {
        const list = await DB('card')
            .join('style', 'card.style', '=', 'style.id')
            .join('kind', 'card.kind', '=', 'kind.id')
            .join('music', 'card.music', '=', 'music.id')
            .select(
                'card.id',
                'card.style',
                'style.name as style_name',
                'card.kind',
                'kind.name as kind_name',
                'card.show',
                'card.title',
                'card.display_url',
                'card.recommend',
                'card.swiper',
                'card.swiper_url',
                'card.wish',
                'card.wish_style',
                'card.music',
                'music.url as music_url',
                'music.name as music_name',
                'card.share_url',
                'card.float_url',
                'card.scroll'
            )
            .limit(ctx.request.body.pageSize)
            .offset((ctx.request.body.pageNumber - 1) * ctx.request.body.pageSize)

        list.forEach(function(item) {
            item.show_name = item.show == 1 ? '是' : '否';
            item.recommend_name = item.recommend == 1 ? '是' : '否';
            item.swiper_name = item.swiper == 1 ? '是' : '否';
        })

        const styleList = await DB('style')
        const musicList = await DB('music')
        const kindList = await DB('kind')
        const wishList = await DB('wish')

        ctx.body = {
            list: list,
            baseData: {
                styleList,
                musicList,
                kindList,
                wishList
            }
        }
    } else if (ctx.request.body.code == 1) {
        delete ctx.request.body.code
        delete ctx.request.body.folder
        ctx.body = await DB('card').insert(ctx.request.body)
    } else if (ctx.request.body.code == 2) {
        const param = ctx.request.body
        const urlList = []
        const backList = []
        if (param['float_url']) {
            urlList.push('float_url')
        }
        if (param['display_url']) {
            urlList.push('display_url')
        }
        if (param['swiper_url']) {
            urlList.push('swiper_url')
        }
        if (param['share_url']) {
            urlList.push('share_url')
        }
        if (urlList.length > 0) {
            const temp = await DB('card').where('id', param.id)
            backList.push(temp[0])
        }
        delete param.code
        delete param.folder
        const isok = await DB('card').where('id', param.id).update(param)
        if (isok === 1) {
            urlList.forEach(function(item) {
                const fpath = path.join(__dirname, `../public/${backList[0][item]}`)
                if (fs.existsSync(fpath)) {
                    fs.unlinkSync(fpath)
                }
            })
        }
        ctx.body = isok
    } else if (ctx.request.body.code == 3) {
        const isok = await DB('card').where('id', ctx.request.body.id).del()
        if (isok === 1) {
            await DB('image').where('card', ctx.request.body.id).del()
            const fpath = path.join(__dirname, `../public/images/${ctx.request.body.id}`)
            if (fs.existsSync(fpath)) {
                const files = fs.readdirSync(fpath)
                files.forEach(function(file, index) {
                    fs.unlinkSync(`${fpath}/${file}`)
                })
                fs.rmdirSync(fpath)
            }
        }
        ctx.body = isok
    }

}